//
//  general data.m
//  ehubApp
//
//  Created by AonePOS on 24/09/13.
//  Copyright (c) 2013 AonePOS. All rights reserved.
//

#import "general data.h"
#import "DataBaseManager.h"

@implementation general_data


@synthesize deviceID,OriginalbaseURLstr,randomString,Datestring,userID,userType,SessionID,inventory_permission,invoice_discounts_permission,invoice_price_change_permission,customer_permission,end_cash_transaction_permission,end_transaction_permission,force_credit_cards_permission,allow_exit_permission,settings_permission,delete_items_permission,hold_print_permission,reports_permission,vendor_payouts_permission,void_invoices_permission,switchValue,loginSessionID;



-(void)GetuserDataVariablesMethod
{
    OriginalbaseURLstr=@"";
    NSString *selectQuery=[NSString stringWithFormat:@"select pageurl,userType,sessionId,userID from miscellaneous"];
    NSMutableArray *mislaneousArray=[[NSMutableArray alloc]init];
    
    [[DataBaseManager dataBaseManager] execute:selectQuery resultsArray:mislaneousArray];
    
   //// NSLog(@"%@",mislaneousArray);
    NSString *pageUrl=@"";
    if ([mislaneousArray count]>0)
    {
        
        for (NSDictionary *Dict in mislaneousArray)
        {
            pageUrl=[Dict objectForKey:@"pageurl"];
             userType=[Dict objectForKey:@"userType"];
             SessionID=[Dict objectForKey:@"sessionId"];
             userID=[Dict objectForKey:@"userID"];
            
            
        }
    }
    int randNum = arc4random()%100000; //create the random umber.
    OriginalbaseURLstr=pageUrl;
    
    NSString *num = [NSString stringWithFormat:@"%d", randNum]; //Make the number into a string.
    deviceID=num;
    
    NSDate *aDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterShortStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *datestr;
    datestr=[formatter stringFromDate:aDate];
    
    //NSLog(@"date string is %@",datestr) ;
   Datestring=datestr;
    
    
    NSString *querySQL = [NSString stringWithFormat:@"SELECT emp_inventory,emp_customers,emp_reports,emp_discounts,emp_settings,emp_price_hanges,emp_allow_exit,emp_vendor_payouts,emp_delete_items,emp_void_invoices,emp_transactions,emp_holdprint,emp_creditcards,emp_endcash,employee_employee_id,unique_id,created_timestamp,modified_timestamp,server_local FROM employee_permissions WHERE employee_employee_id='%@'",userID];
    NSMutableArray *employeepermissionsArray=[[NSMutableArray alloc]init];
    [[DataBaseManager dataBaseManager]execute:querySQL resultsArray:employeepermissionsArray];
    @autoreleasepool {
        for (NSDictionary *myDict in employeepermissionsArray)
        {
            NSString *EmpID=[myDict objectForKey:@"employee_employee_id"];
            
            if ([EmpID isEqualToString:userID]){
                
                inventory_permission=[myDict objectForKey:@"emp_inventory"];
                
               invoice_discounts_permission=[myDict objectForKey:@"emp_discounts"];
                
                invoice_price_change_permission=[myDict objectForKey:@"emp_price_hanges"];
                
                reports_permission=[myDict objectForKey:@"emp_reports"];
                customer_permission=[myDict objectForKey:@"emp_customers"];
                
                delete_items_permission=[myDict objectForKey:@"emp_delete_items"];
                
                end_cash_transaction_permission=[myDict objectForKey:@"emp_endcash"];
                
                settings_permission=[myDict objectForKey:@"emp_settings"];
                end_transaction_permission=[myDict objectForKey:@"emp_transactions"];
                
                hold_print_permission=[myDict objectForKey:@"emp_holdprint"];
                customer_permission=[myDict objectForKey:@"emp_customers"];
                
                allow_exit_permission=[myDict objectForKey:@"emp_allow_exit"];
                
                vendor_payouts_permission=[myDict objectForKey:@"emp_vendor_payouts"];
                
                void_invoices_permission=[myDict objectForKey:@"emp_void_invoices"];
                
            }
        }
   
    }
}
NSString *letters = @"abcdefghijklmnopqrstuvwxyz";

-(NSString *) genRandStringLength: (int) len {
    
   randomString = [NSMutableString stringWithCapacity: len];
    
    for (int i=0; i<len; i++) {
        [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
    }
    
    return randomString;
}
-(NSString *)getsystemDatetime
{
    NSDate *aDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterShortStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *datestr;
    datestr=[formatter stringFromDate:aDate];
    
    //NSLog(@"date string is %@",datestr) ;
    Datestring=datestr;
    return Datestring;
}
-(NSString *)getDeviceID
{
       int randNum = arc4random()%100000;
    NSString *num = [NSString stringWithFormat:@"%d", randNum]; //Make the number into a string.
    self.deviceID=num;
    //deviceID=num;
    return deviceID;
    
}
-(bool)connectedToInternet
{
	Reachability *r = [Reachability reachabilityForLocalWiFi];//[Reachability reachabilityWithHostName:@"www.google.com"];
	NetworkStatus internetStatus = [r currentReachabilityStatus];
	bool result = false;
	if (internetStatus == ReachableViaWiFi)
	{
	    result = true;
	}
	else if(internetStatus==ReachableViaWWAN){
		result = true;
	}
	return result;
}
-(void)internetAlert{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Internet!" message:@"Not Connected to Internet" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
	[alert show];
}
+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

-(BOOL)releasevariable
{
  userID=nil,userType=nil,SessionID=nil;
    
    
   inventory_permission=nil,reports_permission=nil,settings_permission=nil,customer_permission=nil,allow_exit_permission=nil,delete_items_permission=nil,end_transaction_permission=nil,force_credit_cards_permission=nil,invoice_discounts_permission=nil,invoice_price_change_permission=nil,vendor_payouts_permission=nil,void_invoices_permission=nil,hold_print_permission=nil,end_cash_transaction_permission=nil;
    
    switchValue=nil;
    
   // loginSessionID=nil;
    return YES;
}

@end
